package com.skillworld.webapp.model.languageservice;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.skillworld.webapp.model.language.Language;
import com.skillworld.webapp.model.language.LanguageDao;




@Service("LanguageService")
public class LanguageServiceImpl implements LanguageService
{
    
    @Autowired
    private LanguageDao languageDao;

    /*
     * TO-DO: Remove Language entity, just load supported-languages from DB.
     */
	@Transactional(readOnly=true)
	public List<Locale> listSupportedLanguages()
	{
	    List<Language> codes = languageDao.findAll();
	    
	    List<Locale> locales = new ArrayList<Locale>();
	    for(Language l : codes)
	    { locales.add(new Locale(l.getCode())); }
	    
	    return locales;
	}
	
	/*
	 * TO-DO?: Add variants like en_US or en_GB ?
	 */
    public List<Locale> listISOLanguages()
    {
       List<Locale> locales = new ArrayList<Locale>();
       for(String code : Locale.getISOLanguages())
       { locales.add(new Locale(code)); } 
       
       return locales;
    }

    public boolean supportedIsoLanguage(String lang){
        boolean supportedLanguage = false;
        
        for(String iso :Locale.getISOLanguages()){
           if(iso.equals(lang)) {supportedLanguage=true;break;} 
        }
        
        return  supportedLanguage;
    }
}
